package com.guigu.pd.base.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.entity.TruckTypeEntity;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.base.dao.TransportLineTypeDao;
import com.guigu.pd.base.entity.TransportLineTypeEntity;
import com.guigu.pd.base.service.TransportLineTypeService;


@Service("transportLineTypeService")
public class TransportLineTypeServiceImpl extends ServiceImpl<TransportLineTypeDao, TransportLineTypeEntity> implements TransportLineTypeService {

    @Autowired
    CoreOrgFeign coreOrgFeign;

    @Override
    public PageVo<TransportLineTypeEntity> findTransportLineTypeByPage(Map<String, Object> params) {
        TransportLineTypeEntity
                transportLineType = JSON.parseObject(JSON.toJSONString(params),
                TransportLineTypeEntity.class);

        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        QueryWrapper<TransportLineTypeEntity> queryWrapper = new QueryWrapper<TransportLineTypeEntity>();

        if (StringUtils.isNotBlank(transportLineType.getName())) {
            queryWrapper.like("name", transportLineType.getName());
        }

        if (StringUtils.isNotBlank(transportLineType.getTypeNumber())) {
            queryWrapper.like("type_number", transportLineType.getTypeNumber());
        }

        if (StringUtils.isNotBlank(transportLineType.getStartAgencyType())) {
            queryWrapper.eq("start_agency_type", transportLineType.getStartAgencyType());
        }
        if (StringUtils.isNotBlank(transportLineType.getEndAgencyType())) {
            queryWrapper.eq("end_agency_type", transportLineType.getEndAgencyType());
        }

        PageHelper.startPage(pageno, pagesize);

        List<TransportLineTypeEntity> list = this.list(queryWrapper);

        for (TransportLineTypeEntity lineType : list) {
            if (lineType.getStartAgencyType()!=null) {
                String startagency = coreOrgFeign.getcornamebyid(lineType.getStartAgencyType());
                lineType.setStartAgencyTypename(startagency);
            }
            if(lineType.getEndAgencyType()!=null) {
                String endagency = coreOrgFeign.getcornamebyid(lineType.getEndAgencyType());
                lineType.setEndAgencyTypename(endagency);
            }
        }

        PageInfo<TransportLineTypeEntity> pageInfo = new PageInfo<>(list);

        PageVo<TransportLineTypeEntity> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }
}